/* Copyright (c) 2009 Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "iaupdateclientapp.h"

_LIT(KFileName, "python25.zip");
_LIT(KFilePath, "c:\\resource\\Python25\\");

const TUid KIAParamUid = { ${{PYS60_UID_CORE}} };

EXPORT_C void IADClientUpdate()
{
    CIAUpdateClientApp *iadUpdateClient = NULL;
    TInt error = KErrNone;

    TRAP(error,iadUpdateClient = CIAUpdateClientApp::NewL());
    if (error != KErrNone)
        return;

    iadUpdateClient->CheckUpdates();

    delete iadUpdateClient;
    iadUpdateClient = NULL;
}

CIAUpdateClientApp* CIAUpdateClientApp::NewL()
{
   CIAUpdateClientApp *self = new (ELeave) CIAUpdateClientApp;
   CleanupStack::PushL(self);
   self->ConstructL();
   CleanupStack::Pop();
   return self;
}

void CIAUpdateClientApp::ConstructL()
{
    iUpdate = NULL;
    forcefulUpdate = EFalse;
    RFs aFs;

    TRAPD(error, aFs.Connect());
    if (error == KErrNone)
    {
        TFindFile findFile(aFs);
        if (findFile.FindByPath(KFileName, &KFilePath) == KErrNotFound)
            forcefulUpdate = ETrue;
        aFs.Close();
    }

    CActiveScheduler::Add(this);
    TRAP_IGNORE(iUpdate = CIAUpdate::NewL( *this ) );
    if (iUpdate) // IAUpdate is supported in the phone
    {
        CleanupStack::PushL(iUpdate);
        iParameters = CIAUpdateParameters::NewL();
        CleanupStack::PushL(iParameters);
        iParameters->SetUid( KIAParamUid );
        CleanupStack::Pop(2);
    }
}

CIAUpdateClientApp::~CIAUpdateClientApp()
{
    if (iUpdate)
    {
        delete iUpdate;
        iUpdate = NULL;
    }
    if (iParameters)
    {
        delete iParameters;
        iParameters = NULL;
    }
}

void CIAUpdateClientApp::CheckUpdates()
{
    iUpdate->CheckUpdates( *iParameters );
    iWait.Start();
}

void CIAUpdateClientApp::CheckUpdatesComplete(TInt aErrorCode,
                                              TInt aAvailableUpdates)
{
    if ( aErrorCode == KErrNone )
    {
        if ( aAvailableUpdates > 0 || forcefulUpdate)
            iUpdate->ShowUpdates( *iParameters );
        else
        {
            // No updates available. CIAUpdate object could be deleted already
            // now, since we are not going to call anything else from the API.
            // This will also close down the IAUpdate server.
            iWait.AsyncStop();
        }
    }
    else
        iWait.AsyncStop();
}

void CIAUpdateClientApp::UpdateComplete(TInt aErrorCode,
                                        CIAUpdateResult* aResult)
{
    if ( aErrorCode == KErrNone )
    {
        // The update process that the user started from IAUpdate UI is now
        // completed. If the client application itself was updated in the update
        // process, this callback is never called, since the client is not
        // running anymore.
        TInt successCount = aResult->SuccessCount();
    }
    // Ownership was transferred, so this must be deleted by the client
    delete aResult;

    // We do not need the client-server session anymore, let's delete the object
    // to close the session
    iWait.AsyncStop();
}
